package com.situ.stmall.admin.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.sound.midi.MidiDevice.Info;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.situ.stmall.admin.entity.APIResult;
import com.situ.stmall.admin.entity.Root;
import com.situ.stmall.admin.service.RootService;
import com.situ.stmall.admin.util.TokenUtil;
import com.situ.stmall.admin.util.TokenUtil.Token;

@RestController
@RequestMapping("/api/roots")
@CrossOrigin(origins = {"*"})
public class RootController {

	@Autowired
	private RootService rootService;
	
	/**
	 * 登陆——查询
	 */
	@GetMapping
	public APIResult login(Root root, String vercode, String token,
			HttpSession session) {
		//获取参数
		APIResult apiResult = new APIResult();
		//判断vercode是否正确
		Token tkn = TokenUtil.parseToken(token);
		if (tkn.getStatus() == 1) {
			
			apiResult.setStatus(404);
			apiResult.setMsg("验证码已过期");
			return apiResult;
		}
		
		
		
		if (! vercode.toUpperCase().equals(tkn.getCode())) {
			apiResult.setStatus(404);
			apiResult.setMsg("验证码错误");
			
			return apiResult;
		}
		
		//调用service层
		
		try {
			Root loginRoot = rootService.login(root);
			//清空密码
			loginRoot.setPassword(null);
			
			//保存登录信息至token
			String loginToken = TokenUtil.createToken(loginRoot.getRootname());
			
			
			apiResult.setStatus(200);
			apiResult.setData(loginRoot);
			apiResult.setMsg(loginToken);
		} catch (Exception e) {
			// 
			e.printStackTrace();
			
			apiResult.setStatus(404);
			apiResult.setMsg(e.getMessage());
		}
		//返回数据
		
		
		return apiResult;
	}
	
	@GetMapping("/{token}")
	public APIResult get(@PathVariable("token") String strToken) {
		Token token = TokenUtil.parseToken(strToken);
		
		//根据用户名查询当前登录的用户
		Root root = rootService.getByRootName(token.getCode());
		root.setPassword(null);
		return APIResult.ok(root);
	}
	
	/**
	 * 退出
	 */
//	@DeleteMapping
//	public APIResult logout(HttpSession session) {
//		session.invalidate();		
//		return null;
//	}
	
	/**
	 * 修改信息
	 */
	@PutMapping
	public APIResult edit(Root root, String repassword) {
		
		
		APIResult apiResult = new APIResult();
		try {
			Root updataRoot = rootService.editRoot(root, repassword);
			updataRoot.setPassword(null);
			
			
			apiResult.setStatus(200);
			apiResult.setData(updataRoot);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			apiResult.setStatus(404);
			apiResult.setMsg(e.getMessage());
		}
		return apiResult;
	}
	
	/**
	 * 用户注册
	 */
	@PostMapping
	public APIResult reg(Root root,String repassword) {
		try {
			rootService.reg(root,repassword);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return null;
	}
}
